import { Entity } from "./Entity";
import { Matcher, MatcherAllOf, MatcherAnyOf, MatcherNoneOf } from "./Matcher";
import { World } from "./World";




/**
 * 系统
 */
export abstract class System {

    /**
     * 优先级
     */
    priority: number = 1000;

    /**
     * 是否使用脏数据
     */
    useDirty: boolean = false;

    /**
     * 匹配器
     */
    matcher: Matcher;

    /**
     * 所属世界
     */
    world: World;

    constructor(allOfAny: MatcherAllOf | MatcherAnyOf, matcherAnyOf?: MatcherAnyOf, none?: MatcherNoneOf, useDirty: boolean = false) {
        this.matcher = new Matcher(allOfAny, matcherAnyOf, none);
        this.useDirty = useDirty;
    }


    tick(dt: number): void {
        if (this.matcher.entitys.size == 0) return;
        this.onTick(dt);
        if (this.useDirty) {
            this.matcher.entitys.clear();
        }
    }

    protected onTick(dt: number): void {
        throw new Error("Method not implemented.");
    }

    get entitys(): Set<Entity> {
        return this.matcher.entitys;
    }

    destroy(): void {
        this.matcher.destroy();
        this.matcher = null;
        this.world = null;
    }
}